package basicservicelogic

import (
	"context"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/result"

	"github.com/zeromicro/go-zero/core/logx"
)

type HospitalDepartmentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHospitalDepartmentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HospitalDepartmentAddLogic {
	return &HospitalDepartmentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// HospitalDepartmentAdd 创建医院科室表项
func (l *HospitalDepartmentAddLogic) HospitalDepartmentAdd(in *property_service.HospitalDepartment) (*property_service.ResponseId, error) {
	// 获取医院科室表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).HospitalDepartment
	// 创建一个新的医院科室表模型对象，不包含ID、TenantID、CreatedAt、UpdatedAt字段
	mod := &model.HospitalDepartment{
		ParentID:          in.ParentID,          // 父级ID
		ParentCode:        in.ParentCode,        //父级编码
		ParentLs:          in.ParentLs,          // 父级列表
		DepartmentCode:    in.DepartmentCode,    // 科室编号
		HisDepartmentCode: in.HisDepartmentCode, // HIS科室编码
		Name:              in.Name,              // 科室名称
		DepartmentType:    in.DepartmentType,    // 科室类型
		PinyinCode:        in.PinyinCode,        // 拼音简码
		Director:          in.Director,          // 科室负责人
		MaintenanceAuto:   in.MaintenanceAuto,   // 维修自动处理
		Status:            in.Status,            // 状态
		Remark:            in.Remark,            // 备注
		StockID:           in.StockID,           // 仓库ID
		TenantID:          in.TenantID,          // 租户ID
		CreatedAtuser:     in.CreatedAtuser,     // 创建用户
		UpdatedAtuser:     in.UpdatedAtuser,     // 更新用户
	}
	// 使用上下文执行创建操作
	if err := m.WithContext(l.ctx).Create(mod); err != nil {
		res, err := result.Err(err).Ed()
		return l.buildResponse(mod.ID, res, err)
	}
	// 创建成功，返回成功响应
	res, err := result.Suc().AddEd()
	return l.buildResponse(mod.ID, res, err)
}

// 在 ContractMainAddLogic 结构体中添加辅助方法
func (l *HospitalDepartmentAddLogic) buildResponse(id int64, res *property_service.Response, err error) (*property_service.ResponseId, error) {
	return &property_service.ResponseId{
		Id:      id,
		Code:    res.Code,
		Message: res.Message,
	}, err
}
